
import turtle as nms
import random
poses = [[0, 20],[0,0],[0,-20]]

p = (random.randint(-360,360), random.randint(-360,360))
a = 20
l = 1
def OK():
    nms.clear()
    for pos in poses:

        nms.up()
        nms.goto(pos)
        nms.dot(20,"red")
    nms.ontimer(OK, 300)
    pos = poses[0].copy()
    pos[l]+=a
    poses.insert(0,pos)
    poses.pop(-1)

    nms.goto(p)
    nms.dot(20, 'blue')

def right():
    global l, a
    l = 0
    a = 20

def left():
    global l, a
    l = 0
    a = -20

def up():
    global a, l
    l = 1
    a = 20

def down():
    global a, l
    l = 1
    a = -20


nms.tracer(False)
OK()
nms.onkeypress(up, 'Up')
nms.onkeypress(down,'Down')
nms.onkeypress(left,'Left')
nms.onkeypress(right, 'Right')
nms.listen()
nms.done()